﻿using Spring.Context.Support;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Xmart.Models.Demo;

namespace Xmart.Win.Demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            var ctx = ContextRegistry.GetContext();



            var action = ctx.GetObject<Xmart.Action.ModelAction>("modelAction");

            var ar = action.Execute("ExecuteQuery", "from FsInfo c where c.FsCode=?", new object[] { "nribdbi" });
            // var ar = action.ExecuteQuery("from FsInfo c where c.FsCode=?", "nribdbi");

            var res = Xmart.Common.DataUtils.To<System.Collections.IList>(ar.Data);





            var dao = ctx.GetObject<ModelTemplate>();
            var fsInfo = dao.ExecuteQuery<FsInfo>();
            var dma = dao.ExecuteQuery<FsInfo>(
                "from FsInfo c where c.FsCode=?", "nribdma");

            var dbi = dao.ExecuteQuery<FsInfo>(
                "from FsInfo c where c.FsCode=:fscode", new string[] { "fscode" }, new string[] { "nribdbi" });

            var fs1 = dao.ExecuteCallback<FsInfo>(
                (s) =>
                {
                    var list = s.CreateQuery("from FsInfo c where c.FsCode=:fsCode And c.Password=:password").SetString("fsCode", "nribdbi").SetString("password", "123456").List<FsInfo>();
                    if (list.Count > 0)
                        return list[0];
                    return null;
                });
            fs1.SmsAddress = "Test Save";
            dao.ExecuteSaveOrUpdate(fs1);

            dao.ExecuteTransaction((s) =>
            {
                dma[0].SmsAddress = "Test Trans";
                fsInfo[0].SmsAddress = "Test Trans";

                s.SaveOrUpdate(dma[0]);
                s.SaveOrUpdate(fsInfo[0]);
            });
        }
    }
}
